<?php
/*
 * 汽车图片
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 108f736db5421c84e17ce9c28e5342dea0a634e3 $
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 汽车图片类
 * Class AutoPictureModule
 */
class LoanModule extends BasicModule {

    /** @var 模块id */
    private $_moduleid;

    /** @var 条目ID */
    private $_itemid;

    /** @var string 表格 */
    private $_table;

    /** @var array 可用字段 */
    private $_fields;

    /**
     * 构造函数
     * @param $moduleid
     */
    function __construct($moduleid) {
        global $db;
        parent::__construct();
        $this->_moduleid = $moduleid;
        $this->_table    =  $db->getPre().'loan';
        $this->_fields   = array(
            'catid',
            'level',
            'title',
            'introduce',
            'keyword',
            'hits',
            'thumb',
            'addtime',
            'level',
            'editor',
            'edittime',
            'linkurl',
            'status'

        );
    }

    public function setItemid($itemid) {

        $this->_itemid = $itemid;

    }

    public function getItemid() {
        return $this->_itemid;
    }

    public function getFields() {
        return $this->_fields;
    }

    /**
     * 是否通过插入检测
     * @param $post
     * @return bool
     */
    function pass($post) {
        if (!is_array($post)) return false;
        if (strlen($post['title']) < 3) return $this->err('请填写标题');
        return true;
    }

    /**
     * 设置符合内容的值
     * @param $post
     * @return array|mixed
     */
    function set($post) {
        global $DT_TIME, $DT_IP, $_username, $_userid;
        $thumbSeName         = $this->_moduleid . '_thumb';
        $post['addtime']     = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
        $post['edittime']    = $DT_TIME;
        //  $post['amid']        = intval($post['amid']);
        $post['title']       = trim($post['title']);
        //  $post['description'] = trim($post['description']);
        if ($this->_itemid) {
            // 修改信息
            $r              = $this->getOne();
            $post['editor'] = $_username;
	        ThumbUpload::clearPicture($thumbSeName, $post['thumb'], $r['thumb'], $_userid);
        } else {
            $post['username'] = $post['editor'] = $_username;
            $post['ip']       = $DT_IP;
	        ThumbUpload::clearPicture($thumbSeName, $post['thumb'], '', $_userid);
        }
        return $post;
    }

    /**
     * 获取一条数据
     * @return array|void
     */
    function getOne() {

        return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$this->_itemid");

    }

    /**
     * 获取列表
     * @param string $condition
     * @param string $order
     * @param string $cache
     * @return array
     */
    function getList($condition = 'status=3', $order = 'addtime DESC', $cache = '') {
        global $items;
        $num   = $this->_db->count($this->_table, $condition, $cache);
        $items = $num;
//        $this->_pagesize=8;
        $this->_pn->recordsPerPage($this->_pagesize);
        $this->_pn->records($num);
        $sql = "SELECT * FROM {$this->_table}";
        $sql .= $condition ? " WHERE {$condition}" : '';

        $sql .= $order ? " ORDER BY {$order}" : '';

        $sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;

        $lists  = $catids = $CATS = array();

        $result = $this->_db->query($sql, $cache);

        while ($r = $this->_db->fetchArray($result)) {
            $r['edittime']       = timetodate($r['edittime'], 4);
            $r['alt']            = $r['title'];
            $r['title']          = set_style($r['title'], $r['style']);
            $catids[$r['catid']] = $r['catid'];
            $lists[]             = $r;
        }
//        return $sql;
        return $lists;
    }

    /**
     * 添加数据
     * @param $post
     * @return int|string
     */
    function add($post) {
        $post = $this->set($post);

        $this->_db->query(Sql::insert($this->_table, $post, $this->_fields));




        return $this->_db->insertId();
    }

    /**
     * 编辑
     * @param $post
     * @return bool
     */
    function edit($post) {
        $post = $this->set($post);

        $sql  = Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields);
        $this->_db->query($sql);
        return true;
    }

    /**
     * 更新
     * @param        $apid
     * @param array  $item
     */
    function update($itemid, $item = array()) {
        $item or $item = $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$itemid");
    }

    /**
     * 回收站
     * @param $apid
     * @return bool
     */
    function recycle($apid) {
        if (is_array($apid)) {
            foreach ($apid as $v) {
                $this->recycle($v);
            }
        } else {
            $this->_db->query(Sql::update($this->_table, "status=0", "apid=$apid"));
            $this->delete($apid, false);
            return true;
        }
        return true;
    }

    /**
     * 还原为默认状态
     * @param $apid
     * @return bool
     */
    function restore($itemid) {
        if (is_array($itemid)) {
            foreach ($itemid as $v) {
                $this->restore($v);
            }
        } else {
            $this->_db->query(Sql::update($this->_table, "status=3", "itemid=$itemid"));
            return true;
        }
        return true;
    }

    /**
     * 删除条目
     * @param      $apid
     */
    function delete($itemid) {
        if (is_array($itemid)) {
            foreach ($itemid as $v) {
                $this->delete($v);
            }
        } else {
            $this->_itemid = $itemid;
            $r           = $this->getOne();;
            Picture::deleteAll($r['thumb']);
            $this->_db->query(Sql::delete($this->_table, "itemid=$itemid"));
        }
    }

    /**
     * 通过审核
     * @param $apid
     * @return bool
     */
    function check($itemid) {
        global $_username, $DT_TIME;
        if (is_array($itemid)) {
            foreach ($itemid as $v) {
                $this->check($v);
            }
        } else {
            $this->_apid = $itemid;
            $this->_db->query(Sql::update($this->_table, "status=3,hits=hits+1,editor='$_username',edittime=$DT_TIME", "itemid=$itemid"));
            return true;
        }
        return true;
    }

    /**
     * 未通过审核
     * @param $apid
     * @return bool
     */
    function reject($itemid) {
        global $_username;
        if (is_array($itemid)) {
            foreach ($itemid as $v) {
                $this->reject($v);
            }
        } else {
            $this->_db->query(Sql::update($this->_table, "status=1,editor='$_username'", "itemid=$itemid"));
            return true;
        }
        return true;
    }

    /**
     * 全部删除回收站里的内容
     * @param string $condition
     */
    function clear($condition = 'status=0') {
        $result = $this->_db->query("SELECT itemid FROM {$this->_table} WHERE $condition ");
        while ($r = $this->_db->fetchArray($result)) {
            $this->delete($r['itemid']);
        }
    }

    /**
     * 级别设置
     * @param $apid
     * @param $level
     */
    function level($itemid, $level) {
        $itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
        $this->_db->query(Sql::update($this->_table, "level=$level", "apid IN ($itemids)"));
    }
}